package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.commonutils.Echarts;
import com.lh.commonutils.R;
import com.lh.entity.EduVideo;
import com.lh.dao.EduVideoMapper;
import com.lh.feign.VodFeign;
import com.lh.service.IEduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author yms
 * @since 2021-06-05
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements IEduVideoService {

    @Resource
    private EduVideoMapper videoMapper;

    @Resource
    private VodFeign vodFeign;

    /**
     * 删除小节
     *
     * @param id
     * @return
     */
    @Override
    public R deleteVideo(String id) {

        EduVideo eduVideo = videoMapper.selectById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            vodFeign.removeALYVideo(videoSourceId);
        }
        int i = videoMapper.deleteById(id);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    public List<Echarts> playout(String begin,String end) {

        List<EduVideo> eduVideos=null;
        if (begin==null && end==null){
            eduVideos = videoMapper.selectList(null);
        }else {
            LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
            wrapper.between(EduVideo::getGmtModified,begin,end);
            eduVideos = videoMapper.selectList(wrapper);

        }

        ArrayList<Echarts> echarts = new ArrayList<>();

        for (EduVideo video : eduVideos) {
            String title = video.getTitle();
            Long playCount = video.getPlayCount();
            Echarts echart = new Echarts();
            echart.setName(title);
            echart.setValue(playCount);
            echarts.add(echart);
        }

        return echarts;
    }

    @Override
    public List<Echarts> findPlayout() {
        List<EduVideo> eduVideos = videoMapper.selectList(null);
        ArrayList<Echarts> echarts = new ArrayList<>();

        for (EduVideo video : eduVideos) {
            String title = video.getTitle();
            Long playCount = video.getPlayCount();
            Echarts echart = new Echarts();
            echart.setName(title);
            echart.setValue(playCount);
            echarts.add(echart);
        }

        return echarts;
    }

    @Override
    public void updateVideoInfo(String videoSourceId) {
        LambdaQueryWrapper<EduVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduVideo::getVideoSourceId,videoSourceId);
        List<EduVideo> list = baseMapper.selectList(wrapper);
        String id = list.get(0).getId();
        baseMapper.updateInfo(id);

    }


}
